﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormStore.实体库;
using WinFormStore.通信层;

namespace WinFormStore.数据层
{
    public class ScannerInfoService
    {
        [Description("读取配置文件,返回一个通信信息对象")]
        public ScannerInfo GetScannerInfoFromPath(string path)
        {
            ScannerInfo scannerInfo = new ScannerInfo();
            //scannerInfo.PortName =
            //    IniConfigHelper.ReadIniData("配置信息", "端口号", "COM19", path);
            //scannerInfo.BaudRate = Convert.ToInt32(
            //    IniConfigHelper.ReadIniData("配置信息", "波特率", "9600", path));
            //scannerInfo.Parity = (Parity)Enum.Parse(typeof(Parity),
            //   IniConfigHelper.ReadIniData("配置信息", "校验位", "None", path), true);
            //scannerInfo.DataBits = Convert.ToInt32(
            //   IniConfigHelper.ReadIniData("配置信息", "数据位", "8", path));
            //scannerInfo.StopBits = (StopBits)Enum.Parse(typeof(StopBits),
            //   IniConfigHelper.ReadIniData("配置信息", "停止位", "One", path), true);
            scannerInfo.PortName =
    IniConfigHelper.ReadIniData("配置信息", "端口号", scannerInfo.PortName, path);
            scannerInfo.BaudRate = Convert.ToInt32(
                IniConfigHelper.ReadIniData("配置信息", "波特率", scannerInfo.BaudRate.ToString(), path));
            scannerInfo.Parity = (Parity)Enum.Parse(typeof(Parity),
               IniConfigHelper.ReadIniData("配置信息", "校验位", scannerInfo.Parity.ToString(), path), true);
            scannerInfo.DataBits = Convert.ToInt32(
               IniConfigHelper.ReadIniData("配置信息", "数据位", scannerInfo.DataBits.ToString(), path));
            scannerInfo.StopBits = (StopBits)Enum.Parse(typeof(StopBits),
               IniConfigHelper.ReadIniData("配置信息", "停止位", scannerInfo.StopBits.ToString(), path), true);
            return scannerInfo;
        }
        [Description("将通信信息对象写入配置文件路径中")]
        public bool SetScannerInfoToPath(ScannerInfo scannerInfo, string path)
        {
            bool result = true;
            result &= IniConfigHelper.WriteIniData("配置信息", "端口号", scannerInfo.PortName, path);
            result &= IniConfigHelper.WriteIniData("配置信息", "波特率", scannerInfo.BaudRate.ToString(), path);
            result &= IniConfigHelper.WriteIniData("配置信息", "校验位", scannerInfo.Parity.ToString(), path);
            result &= IniConfigHelper.WriteIniData("配置信息", "数据位", scannerInfo.DataBits.ToString(), path);
            result &= IniConfigHelper.WriteIniData("配置信息", "停止位", scannerInfo.StopBits.ToString(), path);
            return result;
        }


        //private ScannerComLib ScannerCom = new ScannerComLib();
        //[Description("串口连接")]
        //public void Connect(ScannerInfo scannerInfo)
        //{
        //    ScannerCom.OpenCom(scannerInfo.PortName, scannerInfo.BaudRate, scannerInfo.Parity, scannerInfo.DataBits, scannerInfo.StopBits);
        //    ScannerCom.CodeReceived += ScannerCom_CodeReceived;
        //}
        //[Description("串口断开")]
        //public void DisConn()
        //{
        //    ScannerCom.CloseCom();
        //}
        //public event Action<string> CodeReceived;//自定义事件
        //private void ScannerCom_CodeReceived(string code)
        //{
        //    CodeReceived?.Invoke(code);
        //}
    }
}
